﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Lon.IO;
using Lon.DataStore;

namespace Lon.Server
{

    public class NetNodeManager
    {
        private Dictionary<int, NetNode> allNodeDict = new Dictionary<int, NetNode>();
        public int InterfaceId;
        private int CalcDbmId(String dbm)
        {
            int result = 0;
            for (int i = 0; i < dbm.Length && i < 3; i++)
            {
                result += (((byte)dbm[i]) << (8 * i));
            }
            return result;
        }
      
        public void LoadNetNodeFromFile(String fileName)
        {
            IniFile cfg = new IniFile(fileName);
            cfg.Load();

            foreach (String nodeName in cfg.GetAllSectionName())
            {
                NetNode node = new NetNode();
                node.Name = nodeName;
                node.Ip = cfg.GetString(nodeName, "IP");
                node.Dbm = cfg.GetString(nodeName, "电报名");
                node.Id = CalcDbmId(node.Dbm);
                node.InferfaceId = this.InterfaceId;
             
                String typeStr = cfg.GetString(nodeName, "计算机类型");
                typeStr = typeStr.ToUpper();
                if (typeStr == "ASERVER")
                {
                    node.NodeType = NodeType.AppServer;
                }
                else if (typeStr == "DSERVER")
                {
                    node.NodeType = NodeType.DataServer;
                }
                else if (typeStr == "FSERVER")
                {
                    node.NodeType = NodeType.FrontServer;
                }
                else if (typeStr == "MCLIENT")
                {
                    node.NodeType = NodeType.MangerClient;
                }
                else if (typeStr == "STATION")
                {
                    node.NodeType = NodeType.Station;
                }
                else if (typeStr == "CLIENT")
                {
                    node.NodeType = NodeType.Terminal;
                }
                else if (typeStr == "TERMINAL")
                {
                    node.NodeType = NodeType.Terminal;
                }
                if (cfg.GetBool(nodeName, "安装标记", false))
                {
                    this.AddNode(node);
                }
                
            }

        }
    
    

        public NetNode GetNode(int id)
        {
            lock (this.allNodeDict)
            {
              
                if (this.allNodeDict.ContainsKey(id))
                {
                    return this.allNodeDict[id];
                }
            }
            return null;
        }

        public List<NetNode> GetNodeList()
        {
            List<NetNode> list = new List<NetNode>();
            lock (this.allNodeDict)
            {
                list.AddRange(this.allNodeDict.Values);
            }
            return list;
        }

        public List<NetNode> GetNodeList(NodeType nodeType)
        {
            List<NetNode> list = new List<NetNode>();
            lock (this.allNodeDict)
            {
                foreach (NetNode node in this.allNodeDict.Values)
                {
                    if (node.NodeType == nodeType)
                    {
                        list.Add(node);
                    }
                }
            }
            return list;
        }




        internal void Save(string p)
        {
          
        }

        internal void AddNode(NetNode node)
        {
            lock (this.allNodeDict)
            {
                this.allNodeDict[node.Id] = node;
            }
        }
    }
}

